<?php


function list_kacamata(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tanggal'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Lensa'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		'Bingkai'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		'+'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'-'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);

	$query = "SELECT * FROM kacamata   ";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =35;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 35; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=kacamata&task=edit_kacamata&id='.$ey['kacamata_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['kacamata_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'tgl'=>  date( 'd-m-Y' , strtotime($ey['tanggal']) ) ,  
			'nik'=>  $karyawan['nik'] ,
			'nama'=>  $karyawan['nama_gelar'] ,
			'lensa'=> position_text_align(  rp_format( $ey['lensa_rupiah'] ),   'right'),  
			'bingkai'=>  position_text_align(   rp_format($ey['bingkai_rupiah'] ),   'right'),  
			'plus'=>  position_text_align(   $ey['gangguan_plus'] ,    'center'),  
			'minus'=>  position_text_align(   $ey['gangguan_minus'] ,    'center'),  
			'ops'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=kacamata&task=edit_kacamata\' });'),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas ,  9 , false ,$paging);
} 

function form_kacamata( $id,$valid_post,$validsubmit){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);
	
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');	

	$view = form_header( "seleski" , "seleski"  );
 	$fields = my_get_data_by_id('kacamata','kacamata_id',$id);
	
	if( $valid_post=='' ){
		$valid_post='submit1';
	}
	$ftanggal = $ftanggalm = $ftanggals = date('d-m-Y');
	if($fields){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$fields['tanggal'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal;
	}	
	$tanggal=(isset($_POST['tanggal'])? $_POST['tanggal'] : $ftanggal );	
	if(  isset($_POST['nama_karyawan'] ) ){
		$fields_nama=$_POST['nama_karyawan'] ;	
		$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);			
	}elseif($fields){
		$karyawan_id = $fields['karyawan_id'] ;
		$field_temp = loaddata_karyawan($karyawan_id);
		$fields_nama= $field_temp['nik'].'/'.$field_temp['nama'] ;
	}else{
		$karyawan_id = 0;
		$fields_nama= "" ;	
	}	

	if(	$valid_post=='submit1' AND !$validsubmit){	
		$temp = array(
			'name'=>'tanggal',
			'value'=>(isset($_POST['tanggal'])? $_POST['tanggal'] : $ftanggal),
			'id'=>'tanggal',
			'type'=>'textfield',
			'size'=>'45'
		);
		$form_temp = form_calendar($temp);
		$view .= form_field_display( $form_temp  , "Tanggal Surat" );
		
		$temp = array(
			'name'=>'nama_karyawan',
			'value'=>( $fields_nama ),
			'id'=>'nama_karyawan' , 
			'type'=>'text',
			'size'=>'45' , 
		);
		$form_temp = form_dynamic($temp);
		$submit = array(
			'value' => ( '  Submit  '),
			'name' => 'submit1', 
			'type'=>'submit'
		);
		$form_submit= form_dynamic($submit); 
		$view .= form_field_display( $form_temp .' '.$form_submit   , "NIK/Nama Karyawan"  );		
	}else{
		$view .= form_field_display( get_form_hidden('tanggal',$_POST['tanggal']).$tanggal ,"Tanggal Surat"  );
		$view .= form_field_display( get_form_hidden('nama_karyawan',$_POST['nama_karyawan']).$fields_nama ,"NIK/Nama Karyawan");
		if(	$valid_post=='submit1' AND $validsubmit){
			$valid_post='submit' ; $validsubmit=false;
		}
	}
	$keluarga_karyawan_id=(isset($_POST['keluarga_karyawan_id'])? $_POST['keluarga_karyawan_id'] : ( $fields ? $fields['keluarga_karyawan_id'] : 0 ) );	
	$lensa=(isset($_POST['lensa'])? $_POST['lensa'] : ( $fields ? $fields['lensa'] : 'T' ) );	
	$bingkai=(isset($_POST['bingkai'])? $_POST['bingkai'] : ( $fields ? $fields['bingkai'] : 'T' ) );	
	$gangguan_plus=(isset($_POST['gangguan_plus'])? $_POST['gangguan_plus'] : ( $fields ? $fields['gangguan_plus'] : ' ' ) );	
	$gangguan_minus=(isset($_POST['gangguan_minus'])? $_POST['gangguan_minus'] : ( $fields ? $fields['gangguan_minus'] : ' ' ) );	
	
	
	if(	$valid_post=='submit' AND !$validsubmit){
		$atemps = array();
		$query = "SELECT a.keluarga_karyawan_id, b.nama,c.label FROM keluarga_karyawan a 
			INNER JOIN biodata b ON b.biodata_id=a.biodata_id 
			INNER JOIN hubungan_keluarga c ON c.hubungan_keluarga_id=a.hubungan_keluarga_id  
			WHERE a.karyawan_id={$karyawan_id} 
			ORDER BY a.hubungan_keluarga_id";	
		
		$re = my_query($query);
		while($row = my_fetch_array($re) ){
			$atemps [$row['keluarga_karyawan_id']] =$row['nama']." ( ".$row['label']." )";
		}
		$temp = array(
			'name'=>'keluarga_karyawan_id',
			'id'=>'keluarga_karyawan_id',
			'value'=>$keluarga_karyawan_id,
		);
		$form_temp = form_dropdown($temp , $atemps);
		$view .= form_field_display( $form_temp , "Nama Pengguna" );

		if($lensa=='Y'){ 
			$temp = array(
				'name'=>'lensa',
				'id'=>'lensa',
				'type'=>'checkbox',
				'checked'=>'checked'
			);
		}else{
			$temp = array(
				'name'=>'lensa',
				'id'=>'lensa',
				'type'=>'checkbox',
			); 
		}
		$form_lensa = form_dynamic($temp).'Lensa';
		if($bingkai=='Y'){ 
			$temp = array(
				'name'=>'bingkai',
				'id'=>'bingkai',
				'type'=>'checkbox',
				'checked'=>'checked'
			);
		}else{
			$temp = array(
				'name'=>'bingkai',
				'id'=>'bingkai',
				'type'=>'checkbox',
			); 
		}
		$form_bingkai = form_dynamic($temp).'Bingkai';

		$form_temp =$form_lensa.' &nbsp; '.$form_bingkai;
		$view .= form_field_display( $form_temp , " " );

		$temp = array(
			'name'=>'gangguan_plus',
			'value'=>$gangguan_plus,
			'id'=>'gangguan_plus','size'=>'8',
			'type'=>'textfield' 
		);
		$form_temp = form_dynamic($temp);
		$view .= form_field_display(  $form_temp , "Gangguan plus"  );
		
		$temp = array(
			'name'=>'gangguan_minus',
			'value'=>$gangguan_minus,
			'id'=>'gangguan_minus','size'=>'8',
			'type'=>'textfield' 
		);
		$form_temp = form_dynamic($temp);
		$view .= form_field_display(  $form_temp , "Gangguan minus"  );
			
		$submit = array(
			'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
			'name' => 'submit', 
			'type'=>'submit','class'=>'main_button'
		);
		$form_submit= form_dynamic($submit); 
		$view .= form_field_display( $form_submit , "&nbsp;" ,  "" );	
	}
	$view .= form_footer( );
	return $view;
}


function valid_kacamata($id){
	$errsubmit = false;
	$err = array();
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	if($karyawan_id == 0){
		$errsubmit = true;
		$err[] = "Karyawan tidak diketahui";
	}
	if(isset($_POST['lensa'])){
		$tgl=last_lensa_bingkai($karyawan_id, $_POST['keluarga_karyawan_id'], 'bingkai',$_POST['tanggal'], $id)	;
		if( isset($tgl) ){			
			$errsubmit = true;
			$err[] ="Jatah sudah diambil pada tanggal ".date( 'd-m-Y' , strtotime($tgl) ) ;
		}
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function submit_kacamata($id = 0){
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	$karyawan = loaddata_karyawan($karyawan_id);
	$status_keluarga_id = ( $_POST['keluarga_karyawan_id']==0 ? 3: 0);
	if( $_POST['keluarga_karyawan_id']==0 ){
		$status_keluarga_id = 0 ;
		$nama_pasien = $karyawan['nama'] ;
	}else{
		$query = "SELECT a.hubungan_keluarga_id, b.nama FROM keluarga_karyawan a 
			INNER JOIN biodata b ON b.biodata_id=a.biodata_id 
			WHERE a.keluarga_karyawan_id={$_POST['keluarga_karyawan_id']}";	
		$result = my_query($query);	
		$keluargas = my_fetch_array($result) ;
		$status_keluarga_id=$keluargas['hubungan_keluarga_id'] ;
		$nama_pasien = $keluargas['nama'] ;
	}
	$lensa = isset($_POST['lensa']) ? 'Y': 'T';
	$bingkai = isset($_POST['bingkai']) ? 'Y': 'T';
	$lensa_rupiah=$bingkai_rupiah='0';
	if( $lensa=='Y'){	
		$lensa_rupiah=get_kolom_konstanta(1); //lensa kacamata
	}	
	if( $bingkai=='Y'){	
		$bingkai_rupiah=get_kolom_konstanta(2); //bingkai kacamata
	}	
	$datakes = array( 
		'tanggal'=>my_type_data_str( form_date_converter($_POST['tanggal']) ), 
		'karyawan_id'=> my_type_data_int($karyawan_id),
		'unit_id' 	=> my_type_data_int('7'),  //7=KACAMATA
		'keluarga_id'  =>my_type_data_int($status_keluarga_id),
		'total_biaya' =>my_type_data_str($lensa_rupiah + $bingkai_rupiah ),	
		'nama_pasien'	=>my_type_data_str($nama_pasien),
	);
	$biaya_id = my_insert_record('kes_biaya_kesehatan',$datakes);
	
	$datas = array(
		'tanggal'=>my_type_data_str( form_date_converter($_POST['tanggal']) ), 
		'biaya_kesehatan_id'=> my_type_data_int($biaya_id),
		'karyawan_id'=> my_type_data_int($karyawan_id),
		'gangguan_plus' => my_type_data_str($_POST['gangguan_plus']),	
		'gangguan_minus'=> my_type_data_str($_POST['gangguan_minus']),
		'lensa' 	=> my_type_data_str($lensa),
		'bingkai' => my_type_data_str($bingkai),
		'keluarga_karyawan_id'=> my_type_data_int($_POST['keluarga_karyawan_id']),
		'lensa_rupiah' 	=> my_type_data_str($lensa_rupiah),
		'bingkai_rupiah' => my_type_data_str($bingkai_rupiah),
	);
	 
	if($id > 0){	
		$t = my_get_data_by_id('kacamata','kacamata_id',$id);
		my_delete_record('kes_biaya_kesehatan','biaya_kesehatan_id',$t['biaya_kesehatan_id']);
		return my_update_record('kacamata','kacamata_id',$id,$datas);
	}
	$new_id = my_insert_record('kacamata',$datas);
	return $new_id;
}

function delete_kacamata($id){
	$t = my_get_data_by_id('kacamata','kacamata_id',$id);
	my_delete_record('kes_biaya_kesehatan','biaya_kesehatan_id',$t['biaya_kesehatan_id']);
	$query = "DELETE FROM kacamata WHERE kacamata_id ={$id} ";
	return my_query($query);
}	

function last_lensa_bingkai($karyawan_id, $keluarga_karyawan_id, $jenis,$tanggal,$kacamata_id){
		
	$query = "SELECT * FROM kacamata WHERE karyawan_id={$karyawan_id}";
	if($keluarga_karyawan_id==0){
		$query .= " AND keluarga_karyawan_id=0";
		if($jenis=="bingkai"){
			$tahun=3;
		}elseif($jenis=="lensa"){
			$tahun=1;
		}
	}else{
		$query .= " AND keluarga_karyawan_id>0";
		$tahun=6;
	}
	if($jenis=="bingkai"){
		$query .= " AND bingkai='Y'";
	}elseif($jenis=="lensa"){
		$query .= " AND lensa='Y'";
	}
	$query .= " AND (tanggal BETWEEN ADDDATE(DATE_ADD('".form_date_converter($tanggal)."', INTERVAL ".-$tahun." YEAR), 1) 
	AND ADDDATE(DATE_ADD('".form_date_converter($tanggal)."', INTERVAL ".$tahun." YEAR), -1))";
	$query .= " AND kacamata_id<>{$kacamata_id}";
	$query .= " ORDER BY tanggal DESC LIMIT 1";
	$result = my_query($query);	
	$fields = my_fetch_array($result);	
	return $fields['tanggal'];
}	


